package com.xhwl.data.util;

import com.alibaba.fastjson.JSONObject;
import com.xhwl.common.exception.BusinessException;
import com.xhwl.common.pojo.cent.sdata.Account;
import com.xhwl.common.utils.ChkUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author meixiong
 */
@Slf4j
public class AccountUtil {

    private final static String ACCOUNT_SAVE_PREFIX = "USERINFO_";

    public static Account userInfo() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
        //来自网关解析token后，往请求头新增的userId
        String userId = request.getHeader("userId");
        String appId = request.getHeader("appId");
        if (ChkUtil.isNull(userId)) {
            throw new BusinessException("userId请先完成登录");
        }
        if (ChkUtil.isNull(appId)) {
            throw new BusinessException("appId请先完成登录");
        }
        RedisUtils redisUtils = SpringContextUtils.getBean(RedisUtils.class);
        Object accountInfo = redisUtils.get(appId + "_" + ACCOUNT_SAVE_PREFIX + userId);
        if (ChkUtil.isNull(accountInfo)) {
            throw new BusinessException("请先完成登录");
        }
        return conversionAccount((JSONObject) accountInfo);
    }


    private static Account conversionAccount(JSONObject jsonObject) {
        Account account = new Account();
        account.setId((Integer) jsonObject.get("userId"));
        account.setIndustryId(jsonObject.getShort("industryId"));
        account.setLoginName(jsonObject.getString("loginName"));
        account.setPassword(jsonObject.getString("password"));
        account.setNickname(jsonObject.getString("nickName"));
        account.setName(jsonObject.getString("name"));
        account.setPhone(jsonObject.getString("phone"));
        account.setSex((Short) jsonObject.get("sex"));
        account.setAge(jsonObject.getInteger("age"));
        account.setFaceId(jsonObject.getString("faceId"));
        account.setFaceUrl(jsonObject.getString("url"));
        account.setStatus((Short) jsonObject.get("status"));
        account.setIsDeleted(jsonObject.getBoolean("isDeleted"));
        account.setUpdatedAt((Date) jsonObject.get("updatedAt"));
        account.setCreatedAt((Date) jsonObject.get("createdAt"));
        return account;
    }

    /**
     * 检查登录态
     *
     * @param account
     * @return void
     * @author meixiong
     * @createDate 2021/5/26 8:47
     * @version: 1.0
     * @status: done
     */
    public static void checkLogin(Account account) {
        if (account == null) {
            throw new BusinessException("请先完成登录");
        }
        if (account.getId() == null) {
            String os = System.getProperty("os.name");
            if (os.toLowerCase().startsWith("win")) {
                account.setId(8085);
            } else {
                throw new BusinessException("无用户ID");
            }
        }
    }
}
